package com.dplus.common.utils.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 *cacheManager：缓存管理器，以前是只允许单例的，不过现在也可以多实例了
 *cache：缓存管理器内可以放置若干cache，存放数据的实质，所有cache都实现了Ehcache接口
 *element：单条缓存数据的组成单位
 * @author jacking.jiao
 */
@Component
public class SpringEhcacheUtil {
    @Autowired(required=true)
    private CacheManager cacheManager;
    
    /**
	 * 根据cache名称和key、value，保存
	 * @param cacheName
	 * @param key
	 * @return
	 */
    public void put(String cacheName, String key, Object value) {  
        Cache cache = getCache(cacheName);  
        cache.acquireWriteLockOnKey(key);
        Element element = new Element(key, value);  
    	try {
    		cache.put(element);  
		} finally {
			cache.releaseWriteLockOnKey(key);
		}
    }  
    /**
     * 根据cache名称和key、value，自定义保存时间，过期自动删除
     * @param cacheName
     * @param key
     * @param value
     * @param timeToIdleSeconds
     * @param timeToLiveSeconds
     */
    public void put(String cacheName, String key, Object value,int timeToIdleSeconds,int timeToLiveSeconds) {  
    	Cache cache = getCache(cacheName);  
    	  cache.acquireWriteLockOnKey(key);
    	Element element = new Element(key, value, timeToIdleSeconds, timeToLiveSeconds);
    	try {
			cache.put(element);
		} finally {
			cache.releaseWriteLockOnKey(key);
		}  
    }  
	/**
	 * 根据cache名称和key，获取
	 * @param cacheName
	 * @param key
	 * @return
	 */
    public Object get(String cacheName, String key) {  
        Cache cache = cacheManager.getCache(cacheName);  
        cache.acquireReadLockOnKey(key);
        Element element;
		try {
			element = cache.get(key);
			return element == null ? null : element.getObjectValue();  
		} finally {
			cache.releaseReadLockOnKey(key);
		}  
    }  

    /**
	 * 根据cache名称和key ，删除
	 * @param cacheName
	 * @param key
	 * @return
	 */
    public void remove(String cacheName, String key) {  
        Cache cache = cacheManager.getCache(cacheName);  
        cache.remove(key);  
    }  
    /**
	 * 根据cache名称，缓存对象
	 * @param cacheName
	 * @return
	 */
    public Cache getCache(String cacheName){
        Cache cache = cacheManager.getCache(cacheName);
        if(cache == null){
            throw new RuntimeException("根据cacheName="+cacheName+"无法获取到对于的cache!请检查配置文件");
        }
        return cache;
    }

	public CacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
    /**
     * 查询缓存key
    * @Title: getListByLike 
    * @Description: TODO(这里用一句话描述这个方法的作用) 
    * @param @param cacheName
    * @param @param likeKey
    * @param @param isStartWith
    * @param @return    设定文件 
    * @return List<Map>    返回类型 
    * @throws
     */
	public   List<Map> getListByLike(String cacheName,String likeKey,boolean isStartWith) {
		CacheManager cacheManager = getCacheManager();
		Cache cache = cacheManager.getCache(cacheName);
	   List<String> l=	cache.getKeys();
	   List<Map> tempList=new ArrayList<>();
	   for(String key:l) {
		   cache.acquireReadLockOnKey(key);
		   Element element = null;
		   Map data;
			try {
				if(isStartWith&&key.startsWith(likeKey)) {	
					element = cache.get(key);
					
				}else {
					if (key.contains(likeKey)) element = cache.get(key);
				}
					
				if(element!=null) {
					data=new HashMap();
					data.put(key, element.getObjectValue());
					tempList.add(data);
				}
				Object o=element == null ? null : key;  
			} finally {
				cache.releaseReadLockOnKey(key);
			}  
		   
		   
		     
	   }
	   return  tempList;
	}
		 
   
}
